﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel;
using AutoMapper;
using Core;
using IglooCoder.Commons.WcfNhibernate;
using Infrastucture;
using WcfService1.DTO;

namespace WcfService1
{
    // NOTE: You can use the "Rename" command on the "Refactor" Salesman to change the class name "SalesmanServices" in code, svc and config file together.
    [ServiceContract(Namespace = "")]
    [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)]
    [NHibernateContext]
    public class SalesmanServices
    {
        private readonly SalesmanRepository SalesmanRepository;

        public SalesmanServices()
        {
            SalesmanRepository = new SalesmanRepository(new WcfSessionStorage());
        }

        [OperationContract]
        public List<EmployeeDTO> GetList(int page, out long recordCount, string sortProperty = null,
                                         string sortType = "ASC")
        {
            var Salesmans = new List<Employee>();
            var Results = new List<EmployeeDTO>();

            if (string.IsNullOrEmpty(sortProperty))
            {
                Salesmans = SalesmanRepository.GetDataPaged(page, 30).ToList();
            }
            else
            {
                Salesmans = SalesmanRepository.GetDataPaged(page, 30, sortProperty, sortType).ToList();
            }

            recordCount = SalesmanRepository.RecordCount;

            Salesmans.ForEach(x =>
                                  {
                                      var SalesmanDto = new EmployeeDTO();
                                      Mapper.Map(x, SalesmanDto);
                                      //AutoMapper.Mapper.Map(x.ParentSalesman, SalesmanDto.ParentSalesman);
                                      Results.Add(SalesmanDto);
                                  }
                );

            return Results;
        }

        [OperationContract]
        public bool Save(EmployeeDTO SalesmanDTO)
        {
            bool res;
            try
            {
                Employee Salesman = SalesmanRepository.GetDataOne(x => x.KodeSalesman == SalesmanDTO.KodeSalesman);
                var user = new User();
                if (Salesman == null)
                {
                    Salesman = new Employee();
                }

                Mapper.Map(SalesmanDTO, Salesman);
                SalesmanRepository.SaveOrUpdate(Salesman);
                res = true;
            }
            catch (Exception)
            {
                res = false;
            }

            return res;
        }

        [OperationContract]
        public bool Delete(EmployeeDTO SalesmanDto)
        {
            bool res;
            try
            {
                var Salesman = new Employee();
                Mapper.Map(SalesmanDto, Salesman);
                SalesmanRepository.Delete(Salesman);
                res = true;
            }
            catch (Exception)
            {
                res = false;
            }

            return res;
        }

        [OperationContract]
        public List<EmployeeDTO> GetListByCriteria(Dictionary<String, Object> criteria, string expression, int page,
                                                   out long recordCount)
        {
            var Salesmans = new List<Employee>();
            var Results = new List<EmployeeDTO>();
            Salesmans = SalesmanRepository.GetDataPaged(criteria, expression, page, 30).ToList();
            recordCount = SalesmanRepository.RecordCount;
            Salesmans.ForEach(x =>
                                  {
                                      var SalesmanDto = new EmployeeDTO();
                                      Mapper.Map(x, SalesmanDto);
                                      Results.Add(SalesmanDto);
                                  }
                );

            return Results;
        }

        [OperationContract]
        public List<EmployeeDTO> GetAllSalesman()
        {
            List<Employee> Salesmans = SalesmanRepository.FetchAll().ToList();
            var Results = new List<EmployeeDTO>();
            Salesmans.ForEach(x =>
                                  {
                                      var SalesmanDto = new EmployeeDTO();
                                      Mapper.Map(x, SalesmanDto);
                                      Results.Add(SalesmanDto);
                                  }
                );

            return Results;
        }

        [OperationContract]
        public EmployeeDTO GetSalesman(String KodeSalesman)
        {
            Employee Salesman = SalesmanRepository.GetDataOne(x => x.KodeSalesman == KodeSalesman);
            var SalesmanDto = new EmployeeDTO();
            Mapper.Map(Salesman, SalesmanDto);
            return SalesmanDto;
        }

        [OperationContract]
        public List<EmployeeDTO> GetSalesmanList(string KodeSalesman)
        {
            List<Employee> Salesmans =
                SalesmanRepository.GetDataList(x => x.KodeSalesman.Contains(KodeSalesman)).ToList();
            var Results = new List<EmployeeDTO>();
            Salesmans.ForEach(x =>
                                  {
                                      var SalesmanDto = new EmployeeDTO();
                                      Mapper.Map(x, SalesmanDto);
                                      Results.Add(SalesmanDto);
                                  }
                );

            return Results;
        }
    }
}